var webpack = require('webpack')
  , path = require('path')
  , glob = require('glob')
  , _ = require('underscore');

module.exports = {
  cache:true,
  // devtool:'cheap-module-source-map',
  recordsPath:path.join(__dirname,'.tmp','recordStats.json'),
  amd:true,
  resolve:{
    alias:{
      'shims':path.join(__dirname, 'node_modules', 'fed-lib/lib', 'shims')
    },
    root: process.cwd(),
    modulesDirectories: ["web_modules", "node_modules", 'node_modules/fed-lib']
  },
  entry:_.extend({
    'vendor':['shims','jquery','underscore','moment','react','react-router','flux','highcharts','numeral'],
    'ui': path.join(__dirname, 'config', 'ui.js'),
    'login': path.join(__dirname, 'config', 'login.js')
  },_.chain(glob.sync('**/main.jsx',{
    cwd: 'modules'
  }))
  .map(function(f){
    return [
      f.replace('.jsx','')
    , path.join(process.cwd(),'modules',f)
    ]
  }).object().value()),
  module: {
    noParse:[/jquery$/,/react-router$/,/vis$/,/local-highcharts$/],
    loaders: [
      {
        test: /\.(jsx|es6)$/,
        exclude: /node_modules\/(?!(fed-lib|react-pagers))/,
        loader: "babel-loader"
      },
      {
        test: /\.less$/,
        loader: "style!css!less"
      },
      {
        test: /\.json$/,
        loader: "json-loader"
      }
    ]
  },
  output: {
    path: path.join(__dirname,'.tmp','js'),
    libraryTarget: 'umd',
    filename: "[name].js"
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery",
      "root.jQuery":'jquery'
    }),
    new webpack.optimize.CommonsChunkPlugin({
      name: "vendor",
      minChunks: Infinity
    })
  ]
};
